package com.movitech.mobile.cz.base.purchase.bizservice.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.movitech.mobile.cz.base.common.aop.ServiceThrowable;
import com.movitech.mobile.cz.base.common.license.LicenseUtil;
import com.movitech.mobile.cz.base.common.util.*;
import com.movitech.mobile.cz.base.modules.config.*;
import com.movitech.mobile.cz.base.purchase.bizservice.PStrategicPurchaseService;
import com.movitech.mobile.cz.base.purchase.bizvo.DealRequestVO;
import com.movitech.mobile.cz.base.purchase.bizvo.PspOverallReqVO;
import com.movitech.mobile.cz.base.purchase.entity.*;
import com.movitech.mobile.cz.base.purchase.entity.ext.*;
import com.movitech.mobile.cz.base.purchase.mapper.ext.ExtPSpOverallPlanDetailMapper;
import com.movitech.mobile.cz.base.purchase.mapper.ext.ExtPSpPlanDetailMapper;
import com.movitech.mobile.cz.base.purchase.mapper.ext.ExtPStrategicPurchaseContactMapper;
import com.movitech.mobile.cz.base.purchase.mapper.ext.ExtPStrategicPurchasePlanMapper;
import com.movitech.mobile.cz.base.purchase.service.*;
import com.movitech.mobile.cz.base.supplier.entity.SCommentConfig;
import com.movitech.mobile.cz.base.supplier.entity.SHistoryComment;
import com.movitech.mobile.cz.base.supplier.entity.SMyDeals;
import com.movitech.mobile.cz.base.supplier.service.SCommentConfigService;
import com.movitech.mobile.cz.base.supplier.service.SHistoryCommentService;
import com.movitech.mobile.cz.base.supplier.service.SMyDealsService;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("pStrategicPurchaseService")
public class PStrategicPurchaseServiceImpl implements PStrategicPurchaseService {

    @Autowired
    private ExtPStrategicPurchasePlanMapper extPStrategicPurchasePlanMapper;
    @Autowired
    private PStrategicPurchaseContactService pStrategicPurchaseContactService;

    @Autowired
    private ExtPSpPlanDetailMapper extPSpPlanDetailMapper;

    @Autowired
    private PSpPlanDetailService pSpPlanDetailService;

    @Autowired
    private PStrategicPurchasePlanService pStrategicPurchasePlanService;

    @Autowired
    private ExtPSpOverallPlanDetailMapper extPSpOverallPlanDetailMapper;

    @Autowired
    private PSpOverallPlanDetailService pSpOverallPlanDetailService;
    @Autowired
    private PSpOverallPlanService pSpOverallPlanService;
    @Autowired
    private PSpOverallPlanAuditsService pSpOverallPlanAuditsService;
    @Autowired
    private SMyDealsService sMyDealsService;

    @Autowired
    private SCommentConfigService sCommentConfigService;

    @Autowired
    private SHistoryCommentService sHistoryCommentService;


    @Override
    public PageCode getPStrategicPurchasePlanList(StrategicPurchaseRequiredFindRequest strategicPurchaseRequiredFindRequest) {
        PageCode pageCode = new PageCode(strategicPurchaseRequiredFindRequest.getPageNumber(), strategicPurchaseRequiredFindRequest.getPageSize());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currentRecordNum", (pageCode.getPageNo() - 1) * pageCode.getPageSize());
        map.put("pageSize", pageCode.getPageSize());

        map.put("planYear", strategicPurchaseRequiredFindRequest.getPlanYear());
        map.put("regionalCompanyId", strategicPurchaseRequiredFindRequest.getRegionalCompanyId());
        map.put("projectId", strategicPurchaseRequiredFindRequest.getProjectId());

        Integer count = extPStrategicPurchasePlanMapper.getPStrategicPurchasePlanListCount(map);
        pageCode.setCount(count == null ? 0 : count);
        List<ExtPStrategicPurchasePlan> ls = extPStrategicPurchasePlanMapper.getPStrategicPurchasePlanList(map);
        pageCode.setList(ls);
        return pageCode;
    }

    @Override
    public PageCode getPStrategicPurchaseRequireList(StrategicPurchaseRequiredFindRequest strategicPurchaseRequiredFindRequest) {
        PageCode pageCode = new PageCode(strategicPurchaseRequiredFindRequest.getPageNumber(), strategicPurchaseRequiredFindRequest.getPageSize());
        Integer count = pSpPlanDetailService.selectExtCount(strategicPurchaseRequiredFindRequest);
        pageCode.setCount(count == null ? 0 : count);
        LicenseUtil.INSTANCE.validate();
        List<ExtPSpPlanDetail> ls = pSpPlanDetailService.selectExtByEntity(strategicPurchaseRequiredFindRequest);
        pageCode.setList(ls);
        return pageCode;
    }

    @Override
    public ExtPStrategicPurchasePlan getById(String id) {
        ExtPStrategicPurchasePlan extPStrategicPurchasePlan = extPStrategicPurchasePlanMapper.getById(id);
        List<ExtPSpPlanDetail> extPSpPlanDetails = extPSpPlanDetailMapper.findByPlanId(id);
        if(extPStrategicPurchasePlan == null){
            extPStrategicPurchasePlan = new ExtPStrategicPurchasePlan();
            extPStrategicPurchasePlan.setRegionalCompanyName(UserUtils.getCurrentUser().getRegionalCompanyName());
            extPStrategicPurchasePlan.setRegionalCompanyId(UserUtils.getCurrentUser().getRegionalCompanyId());
        }
        if(extPSpPlanDetails!=null)extPStrategicPurchasePlan.setPSpPlanDetailList(extPSpPlanDetails);
        return extPStrategicPurchasePlan;
    }

    @Override
    public ExtPStrategicPurchasePlan getByReqEntity(StrategicPurchaseRequiredFindRequest strategicPurchaseRequiredFindRequest) {
        ExtPStrategicPurchasePlan extPStrategicPurchasePlan = extPStrategicPurchasePlanMapper.getByEntity(strategicPurchaseRequiredFindRequest);
//        if(extPStrategicPurchasePlan == null){
//            extPStrategicPurchasePlan = new ExtPStrategicPurchasePlan();
//
//            extPStrategicPurchasePlan.setRegionalCompanyName("山西当代地区公司");
//            extPStrategicPurchasePlan.setRegionalCompanyId("00291e92adc2457aa5231e3a779743a4");
//
////            extPStrategicPurchasePlan.setRegionalCompanyName(UserUtils.getCurrentUser().getRegionalCompanyName());
////            extPStrategicPurchasePlan.setRegionalCompanyId(UserUtils.getCurrentUser().getRegionalCompanyId());
//        }else{
        if(extPStrategicPurchasePlan!=null){
            List<ExtPSpPlanDetail> extPSpPlanDetails = extPSpPlanDetailMapper.findByPlanId(extPStrategicPurchasePlan.getId());
            if(extPSpPlanDetails!=null)extPStrategicPurchasePlan.setPSpPlanDetailList(extPSpPlanDetails);
        }
       // }
        return extPStrategicPurchasePlan;
    }

    @Override
    public boolean deleteById(String id) {
        PStrategicPurchasePlan pStrategicPurchasePlan=new PStrategicPurchasePlan();
        pStrategicPurchasePlan.setId(id);
        LicenseUtil.INSTANCE.validate();
        pStrategicPurchasePlan.setDelFlag(CommonConstants.DEL_FLAG_DELETE);
        return pStrategicPurchasePlanService.updateById(pStrategicPurchasePlan);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackForClassName = {"RuntimeException", "Exception"})
    public boolean deleteDetailById(String id) {
        PSpPlanDetail pSpPlanDetail = pSpPlanDetailService.selectById(id);
        pSpPlanDetail.setDelFlag(CommonConstants.DEL_FLAG_DELETE);
        pSpPlanDetailService.updateById(pSpPlanDetail);

        PSpPlanDetail where = new PSpPlanDetail();
        LicenseUtil.INSTANCE.validate();
        String planId = pSpPlanDetail.getStrategicPurchasePlanId();
        where.setStrategicPurchasePlanId(planId);
        where.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(where);
        if(pSpPlanDetailService.selectCount(entityWrapper)<=0){
            deleteById(planId);
        }

        if(StringUtils.isNotEmpty(pSpPlanDetail.getOverallPlanDetailId())){
            pSpOverallPlanDetailService.addOrReduceurchasedAmount(-pSpPlanDetail.getEngineeringAmount(),pSpPlanDetail.getOverallPlanDetailId());
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackForClassName = {"RuntimeException", "Exception"})
    public boolean saveOrUpdatePStrategicPurchasePlan(ExtPStrategicPurchasePlan extPStrategicPurchasePlan) {
        try {
            StrategicPurchaseRequiredFindRequest strategicPurchaseRequiredFindRequest = new StrategicPurchaseRequiredFindRequest();
            BeanUtils.copyProperties(extPStrategicPurchasePlan,strategicPurchaseRequiredFindRequest);
            strategicPurchaseRequiredFindRequest.setId(null);
            ExtPStrategicPurchasePlan extData = extPStrategicPurchasePlanMapper.getByEntity(strategicPurchaseRequiredFindRequest);
            PStrategicPurchasePlan pStrategicPurchasePlan= null;
            LicenseUtil.INSTANCE.validate();
            if(extData==null){//新增 需求
                pStrategicPurchasePlan = new PStrategicPurchasePlan();
                BeanUtils.copyProperties(extPStrategicPurchasePlan,pStrategicPurchasePlan);
                pStrategicPurchasePlan.setBaseFeilds(UserUtils.getCurrentUserId(),null,0,true);
                pStrategicPurchasePlanService.insert(pStrategicPurchasePlan);
            }else{//修改
                pStrategicPurchasePlan = extData;
                List<ExtPSpPlanDetail> extPSpPlanDetails = extPSpPlanDetailMapper.findByPlanId(extData.getId());
                if(CollectionUtils.isNotEmpty(extPSpPlanDetails)){
                    for(ExtPSpPlanDetail e: extPSpPlanDetails){
                        if(StringUtils.isNotEmpty(e.getOverallPlanDetailId())){
                            pSpOverallPlanDetailService.addOrReduceurchasedAmount(-e.getEngineeringAmount(),e.getOverallPlanDetailId());
                        }
                    }
                    pSpPlanDetailService.deleteByStrategicId(extData.getId());
                }
            }

            List<PSpPlanDetail> ls=ListObjCopyUtil.copyTo(extPStrategicPurchasePlan.getPSpPlanDetailList(), PSpPlanDetail.class);
            PSpOverallPlanDetail where=new PSpOverallPlanDetail();

            Date date = DateUtils.getNow();
            for(PSpPlanDetail obj:ls){
                obj.setId(null);obj.setId(null);
                obj.setStrategicPurchasePlanId(pStrategicPurchasePlan.getId());
                obj.setBaseFeilds(UserUtils.getCurrentUserId(),null,0,true);
                PSpOverallPlanDetail pSpOverallPlanDetail = null;

                LicenseUtil.INSTANCE.validate();
                if(StringUtils.isNotEmpty(obj.getResponsibilityId())){
                    where.setResponsibilityId(obj.getResponsibilityId());
                    where.setPlanYear(pStrategicPurchasePlan.getPlanYear());
                    where.setPurchaseCategoryId(obj.getPurchaseCategoryId());
                    List<PSpOverallPlanDetail> list = pSpOverallPlanDetailService.selectByEntity(where);
                    if(CollectionUtils.isNotEmpty(list)){
                        pSpOverallPlanDetail = list.get(0);
                        pSpOverallPlanDetailService.addOrReduceurchasedAmount(obj.getEngineeringAmount(),pSpOverallPlanDetail.getId());
                    }else{
                        PSpOverallPlan pSpOverallPlan = new PSpOverallPlan();
                        pSpOverallPlan.setBaseFeilds(UserUtils.getCurrentUserId(),"",CommonConstants.DEL_FLAG_NORMAL,true);
                        pSpOverallPlan.setPlanYear(pStrategicPurchasePlan.getPlanYear());
                        pSpOverallPlan.setPlanStatus(PurchaseCommonEnum.P_OVER_PLAN_STATUS_1.getCode());
                        pSpOverallPlan.setGenOverallPlanTime(date);
                        pSpOverallPlanService.insert(pSpOverallPlan);

                        pSpOverallPlanDetail=new PSpOverallPlanDetail();
                        pSpOverallPlanDetail.setBaseFeilds(UserUtils.getCurrentUserId(),"", CommonConstants.DEL_FLAG_NORMAL,true);
                        pSpOverallPlanDetail.setPlanYear(pStrategicPurchasePlan.getPlanYear());
                        pSpOverallPlanDetail.setPurchaseCategoryId(obj.getPurchaseCategoryId());
                        pSpOverallPlanDetail.setPurchaseMajorId(obj.getPurchaseMajorId());
                        pSpOverallPlanDetail.setResponsibilityId(obj.getResponsibilityId());
                        pSpOverallPlanDetail.setPurchasedAmount(obj.getEngineeringAmount());
//                        pSpOverallPlanDetail.setPlanSetupTime(obj.getPlanSetupTime());
//                        pSpOverallPlanDetail.setPlanFinishedTime(obj.getPlanFinishedTime());
//                        pSpOverallPlanDetail.setPlanSignedYears(obj.getPlanSignedYears());
//                        pSpOverallPlanDetail.setPlanPurchaseChannels(obj.getPlanPurchaseChannels());
                        pSpOverallPlanDetail.setPlanStatus(PurchaseCommonEnum.P_OVER_PLAN_STATUS_1.getCode());
                        pSpOverallPlanDetail.setInitProcessFlag(CommonConstants.UNDONG_FLAG);
                        pSpOverallPlanDetail.setPurchaseType(obj.getPlanPurchaseType());
                        pSpOverallPlanDetail.setOverallPlanId(pSpOverallPlan.getId());
                        pSpOverallPlanDetailService.insert(pSpOverallPlanDetail);
                    }

                }
                if(pSpOverallPlanDetail!=null)
                     obj.setOverallPlanDetailId(pSpOverallPlanDetail.getId());
                pSpPlanDetailService.insert(obj);
            }

        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(),e.getCause());
        }
        return true;
    }

    @Override
    public PageCode getPSpOverallPlanList(PStrategicPurchasePlanReq pStrategicPurchasePlanReq) {
        PageCode pageCode = new PageCode(pStrategicPurchasePlanReq.getPageNumber(), pStrategicPurchasePlanReq.getPageSize());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currentRecordNum", (pageCode.getPageNo() - 1) * pageCode.getPageSize());
        map.put("pageSize", pageCode.getPageSize());
        LicenseUtil.INSTANCE.validate();

        map.put("purchaseYear", pStrategicPurchasePlanReq.getPurchaseYear());
        map.put("regionalCompanyId", pStrategicPurchasePlanReq.getRegionalCompanyId());
        map.put("majorIds", pStrategicPurchasePlanReq.getMajorIds());
        map.put("categoryIds", pStrategicPurchasePlanReq.getCategoryIds());

        Integer count = extPSpOverallPlanDetailMapper.getPSpOverallPlanListCount(map);
        pageCode.setCount(count == null ? 0 : count);
        List<ExtPSpOverallPlanDetail> ls = extPSpOverallPlanDetailMapper.getPSpOverallPlanList(map);
        pageCode.setList(ls);
        return pageCode;
    }

    @Override
    public ExtPSpOverallPlanDetail getPSpOverallPlan(String id) {
        ExtPSpOverallPlanDetail extPSpOverallPlanDetail = extPSpOverallPlanDetailMapper.getById(id);
        return extPSpOverallPlanDetail;
    }

    @Override
    public boolean saveOrUpdatePSpOverallPlan(ExtPSpOverallPlanDetail extPSpOverallPlanDetail) {
        LicenseUtil.INSTANCE.validate();
        return pSpOverallPlanDetailService.updateById(extPSpOverallPlanDetail);
    }

    @Override
    public boolean saveOrUpdatePurchaseContact(PStrategicPurchaseContact extPStrategicPurchaseContact) {
        LicenseUtil.INSTANCE.validate();
        return pStrategicPurchaseContactService.insertOrUpdate(extPStrategicPurchaseContact);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackForClassName = {"RuntimeException", "Exception"})
    public boolean generatePSpOverallPlan(PspOverallReqVO pspOverallReqVO) {
        try {
            List<PSpPlanDetail>  list = pSpPlanDetailService.getDetailsByYear(pspOverallReqVO.getYear());
            Date date = new Date();
            for(PSpPlanDetail pd:list){
                PSpOverallPlan pSpOverallPlan = new PSpOverallPlan();
                pSpOverallPlan.setBaseFeilds(UserUtils.getCurrentUserId(),"",CommonConstants.DEL_FLAG_NORMAL,true);
                pSpOverallPlan.setPlanYear(pspOverallReqVO.getYear());
                pSpOverallPlan.setPlanStatus(PurchaseCommonEnum.P_OVER_PLAN_STATUS_1.getCode());
                pSpOverallPlan.setGenOverallPlanTime(date);
                pSpOverallPlanService.insert(pSpOverallPlan);
                LicenseUtil.INSTANCE.validate();

                PSpOverallPlanDetail pSpOverallPlanDetail=new PSpOverallPlanDetail();
                pSpOverallPlanDetail.setBaseFeilds(UserUtils.getCurrentUserId(),"", CommonConstants.DEL_FLAG_NORMAL,true);
                pSpOverallPlanDetail.setPlanYear(pspOverallReqVO.getYear());
                pSpOverallPlanDetail.setPurchaseCategoryId(pd.getPurchaseCategoryId());
                pSpOverallPlanDetail.setResponsibilityId(pd.getResponsibilityId());
                pSpOverallPlanDetail.setPurchasedAmount(pd.getEngineeringAmount());
                pSpOverallPlanDetail.setPlanSetupTime(pd.getPlanSetupTime());
                pSpOverallPlanDetail.setPlanFinishedTime(pd.getPlanFinishedTime());
                pSpOverallPlanDetail.setPlanSignedYears(pd.getPlanSignedYears());
                pSpOverallPlanDetail.setPlanPurchaseChannels(pd.getPlanPurchaseChannels());
                pSpOverallPlanDetail.setPlanStatus(PurchaseCommonEnum.P_OVER_PLAN_STATUS_1.getCode());
                pSpOverallPlanDetail.setInitProcessFlag(CommonConstants.UNDONG_FLAG);
                pSpOverallPlanDetail.setPurchaseType(pd.getPlanPurchaseType());
                pSpOverallPlanDetail.setOverallPlanId(pSpOverallPlan.getId());
                pSpOverallPlanDetailService.insert(pSpOverallPlanDetail);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException(e.getMessage(),e.getCause());
        }
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackForClassName = {"RuntimeException", "Exception"})
    public void launchOverallPlanConfirm(PspOverallReqVO pspOverallReqVO) {
        if(pspOverallReqVO.getYear()==null){
            throw new RuntimeException("参数异常",new ServiceThrowable());
        }
        String userId = UserUtils.getCurrentUserId();
        pspOverallReqVO.setCurrentUser(userId);
        PSpOverallPlanAudits pSpOverallPlanAudits = pSpOverallPlanAuditsService.selectByYear(pspOverallReqVO.getYear());
        if(pSpOverallPlanAudits!=null&&pSpOverallPlanAudits.getAuditsStatus()==PurchaseCommonEnum.PURCHASE_PLAN_PROC_CONFIRM_STATUS_ING.getCode()){
            throw new RuntimeException(pspOverallReqVO.getYear()+"年度总规划在确认中！不要重复发起确认",new ServiceThrowable());
        }
        if(pSpOverallPlanAudits==null){
            LicenseUtil.INSTANCE.validate();
            pSpOverallPlanAudits = generateOveralPlanAudits(pspOverallReqVO);
        }else{
            pSpOverallPlanAudits.setAuditsStatus(1);
            pSpOverallPlanAudits.setUpdateBy(userId);
            pSpOverallPlanAudits.setUpdateDate(DateUtils.getNow());
        }
        pSpOverallPlanAuditsService.insertOrUpdate(pSpOverallPlanAudits);
        pSpOverallPlanDetailService.updatePlanStatusByYear(PurchaseCommonEnum.P_OVER_PLAN_STATUS_1.getCode(),PurchaseCommonEnum.P_OVER_PLAN_STATUS_2.getCode(),pspOverallReqVO.getYear());


        SHistoryComment sHistoryComment = generatePsplanConfirmHistory(pspOverallReqVO);
        sHistoryComment.setUserId(pspOverallReqVO.getCurrentUser());
        sHistoryComment.setActionName(PurchaseActionNameEnum.PURCHASE_PLAN_1_1.getDesc());
        sHistoryComment.setRelatedId(pSpOverallPlanAudits.getId());

        sHistoryCommentService.insert(sHistoryComment);
        sHistoryComment.setId(null);
        sHistoryComment.setBaseFeilds(userId,"",CommonConstants.DEL_FLAG_NORMAL,true);
        sHistoryComment.setUpdateDate(null);
        sHistoryComment.setUserId(pspOverallReqVO.getUserIds());
        sHistoryComment.setActionName(PurchaseActionNameEnum.PURCHASE_PLAN_1_6.getDesc());
        sHistoryCommentService.insert(sHistoryComment);

        LicenseUtil.INSTANCE.validate();
        SMyDeals sMyDeals = generatePsplanConfirmDeal(pspOverallReqVO);
        sMyDeals.setRelatedId(pSpOverallPlanAudits.getId());
        sMyDeals.setSubRelatedId(sHistoryComment.getId());//记录历史id 用于更新历史
        sMyDealsService.insert(sMyDeals);

    }

    public SMyDeals generatePsplanConfirmDeal(PspOverallReqVO pspOverallReqVO){
        SMyDeals planConfirmDeal = new SMyDeals();
        planConfirmDeal.setBaseFeilds(pspOverallReqVO.getCurrentUser(), "", CommonConstants.DEL_FLAG_NORMAL, true);
        planConfirmDeal.setDealType(DealTypeEnum.PURCHASE.getCode());
        planConfirmDeal.setDealSubType(MyDealSubTypeEnum.LAUNCH_OVERALL_PSPLAN_CONFIRM.getCode());
        SCommentConfig sc = sCommentConfigService.getByCode(CommentConfigEnum.PURCHASE_INTRANET_ADD_DEAL_LAUNCH_OVERALL_PSPLAN_CONFIRM.getCode());
        planConfirmDeal.setDealTitle(sc==null?"": MessageFormat.format(sc.getConfigValue(),String.valueOf(pspOverallReqVO.getYear())));
        planConfirmDeal.setDoneFlag(CommonConstants.DEL_FLAG_NORMAL);
        planConfirmDeal.setActionType(MyDealActionTypeEnum.LAUNCH_OVERALL_PSPLAN_CONFIRM.getCode());
        planConfirmDeal.setUserId(pspOverallReqVO.getUserIds());
        LicenseUtil.INSTANCE.validate();
        return planConfirmDeal;
    }

    SHistoryComment generatePsplanConfirmHistory(PspOverallReqVO pspOverallReqVO){
        LicenseUtil.INSTANCE.validate();
        SHistoryComment planConfirmHistory= new SHistoryComment();
        planConfirmHistory.setBaseFeilds(pspOverallReqVO.getCurrentUser(), "", CommonConstants.DEL_FLAG_NORMAL, true);
        planConfirmHistory.setModelSubType(CommonConstants.HISTORY_SUB_TYPE_TASK);
        planConfirmHistory.setModelType(HistoryCommentModelTypeEnum.HistoryCommentModelType28.getCode());
        planConfirmHistory.setCommentType(HistoryCommentTypeEnum.INTRANET.getCode());
        planConfirmHistory.setNodeName(pspOverallReqVO.getYear()+"");
        planConfirmHistory.setSetupTime(DateUtils.getNow());
        return planConfirmHistory;
    }

    public PSpOverallPlanAudits generateOveralPlanAudits(PspOverallReqVO pspOverallReqVO){
        PSpOverallPlanAudits pSpOverallPlanAudits = new PSpOverallPlanAudits();
        pSpOverallPlanAudits.setPlanYear(pspOverallReqVO.getYear());
        pSpOverallPlanAudits.setAuditsStatus(1);
        pSpOverallPlanAudits.setConfirmUserIds(pspOverallReqVO.getUserIds());
        pSpOverallPlanAudits.setBaseFeilds(pspOverallReqVO.getCurrentUser(),"",CommonConstants.DEL_FLAG_NORMAL,true);
        return pSpOverallPlanAudits;
    }

    @Override
    public PageCode overallPlanConfirmDealDtl(PStrategicPurchasePlanReq pStrategicPurchasePlanReq) {
        LicenseUtil.INSTANCE.validate();
        SMyDeals sMyDeals = sMyDealsService.selectById(pStrategicPurchasePlanReq.getDealId());
        PSpOverallPlanAudits pSpOverallPlanAudits = pSpOverallPlanAuditsService.selectById(sMyDeals.getRelatedId());
        if(pSpOverallPlanAudits==null){
            throw new RuntimeException("数据异常！",new ServiceThrowable());
        }

        PageCode pageCode = new PageCode(pStrategicPurchasePlanReq.getPageNumber(), pStrategicPurchasePlanReq.getPageSize());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currentRecordNum", (pageCode.getPageNo() - 1) * pageCode.getPageSize());
        map.put("pageSize", pageCode.getPageSize());
        map.put("purchaseYear", pSpOverallPlanAudits.getPlanYear());
        LicenseUtil.INSTANCE.validate();
        map.put("regionalCompanyId", pStrategicPurchasePlanReq.getRegionalCompanyId());
        map.put("categoryIds", pStrategicPurchasePlanReq.getCategoryIds());
        map.put("majorIds", pStrategicPurchasePlanReq.getMajorIds());

        map.put("statusNotIn",new int[]{PurchaseCommonEnum.P_OVER_PLAN_STATUS_1.getCode()});

        Integer count = extPSpOverallPlanDetailMapper.getPSpOverallPlanListCount(map);
        pageCode.setCount(count == null ? 0 : count);
        List<ExtPSpOverallPlanDetail> ls = extPSpOverallPlanDetailMapper.getPSpOverallPlanList(map);
        pageCode.setList(ls);
        return pageCode;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackForClassName = {"RuntimeException", "Exception"})
    public void confirmOverallPlan(DealRequestVO dealRequestVO) {
        SMyDeals sMyDeals = sMyDealsService.selectById(dealRequestVO.getId());
        LicenseUtil.INSTANCE.validate();
        if(sMyDeals==null||sMyDeals.getDoneFlag()==CommonConstants.DONG_FLAG||sMyDeals.getDelFlag()==CommonConstants.DEL_FLAG_DELETE){
            throw new RuntimeException("代办已处理！！",new ServiceThrowable());
        }
        Date now = DateUtils.getNow();
        String userId = UserUtils.getCurrentUserId();
        PSpOverallPlanAudits pSpOverallPlanAudits = pSpOverallPlanAuditsService.selectById(sMyDeals.getRelatedId());
        pSpOverallPlanAudits.setUpdateDate(now);
        pSpOverallPlanAudits.setUpdateBy(userId);
        pSpOverallPlanAudits.setAuditsStatus(2);
        pSpOverallPlanAuditsService.updateById(pSpOverallPlanAudits);

        pSpOverallPlanDetailService.updatePlanStatusByYearNotIn(PurchaseCommonEnum.P_OVER_PLAN_STATUS_1.getCode(),
                dealRequestVO.getAgreeFlag()==1?PurchaseCommonEnum.P_OVER_PLAN_STATUS_1.getCode():PurchaseCommonEnum.P_OVER_PLAN_STATUS_3.getCode(),pSpOverallPlanAudits.getPlanYear());

        SHistoryComment sHistoryComment = sHistoryCommentService.selectById(sMyDeals.getSubRelatedId());
        sHistoryComment.setCommentContent(dealRequestVO.getMsg());
        LicenseUtil.INSTANCE.validate();
        sHistoryComment.setActionName(dealRequestVO.getAgreeFlag()==CommonConstants.DEAL_OK?PurchaseActionNameEnum.PURCHASE_PLAN_1_2.getDesc(): PurchaseActionNameEnum.PURCHASE_PLAN_1_3.getDesc());
        sHistoryComment.setAgreeFlag(dealRequestVO.getAgreeFlag());
        sHistoryComment.setUpdateBy(userId);
        sHistoryComment.setUpdateDate(now);
        sHistoryCommentService.updateById(sHistoryComment);

        sMyDeals.setDealDoneTime(now);
        sMyDeals.setDoneFlag(CommonConstants.DONG_FLAG);
        sMyDealsService.updateById(sMyDeals);
    }
}
